<template>
	<view class="user-card">
		<ShareCanvas ref="shareCanvas" />
		<view class="title">
			<image class="logo" mode="aspectFill" src="/static/logo.png"></image>
			<view class="text">
				<text class="main">{{setting.companyName}}</text>
				<text class="sub">{{setting.companyNameEn}}</text>
			</view>
		</view>
		<ECard v-if="userInfo.id" :user-info="userInfo" :setting="setting" :security="true" />
		<Comp v-if="setting.id" :setting="setting" />
	</view>
</template>

<script setup>
	import ShareCanvas from '../../components/share.vue'
	import { ref, computed } from 'vue';
	import { onShareAppMessage, onLoad } from '@dcloudio/uni-app'
	import ECard from '../../components/ecard.vue'
	import Comp from '../../components/comp.vue'
	import { useAuthStore } from '../../store/auth';
	import { storeToRefs } from 'pinia';
	import { useECardStore } from '../../store/ecard';
	import { getECardShareData, getUserSetting } from '../../request/api/ecard-share';
	import { buildShareObject } from '../../utils/share';

	// data
	const shareCanvas = ref() // 分享图片
	const userInfo = ref({}) // 用户信息
	const setting = ref({}) // 用户配置

	// methods
	const loadUserInfo = (id) => {
		return new Promise((resolve, reject) => {
			getECardShareData(id).then(data => {
				userInfo.value = data
				resolve(userInfo.value)
			}).catch((e) => {
				userInfo.value = {}
				uni.showToast({
					title: '获取用户信息失败',
					icon: 'error'
				})
				reject()
			})
		})
	}
	const loadUserSetting = (settingId, userId) => {
		return new Promise((resolve, reject) => {
			getUserSetting(settingId, userId).then(data => {
				setting.value = data
				resolve(setting.value)
			}).catch((e) => {
				setting.value = {}
				uni.showToast({
					title: '获取配置信息失败',
					icon: 'error'
				})
				reject()
			})
		})
	}


	// lifecircle
	onLoad((opts) => {
		// 未授权用户
		if (!useAuthStore().rtoken) {
			uni.hideHomeButton()
		}
		const { uid, sid } = opts || {}
		if (!uid || !sid) return
		uni.showLoading({
			title: '加载中……'
		})
		loadUserInfo(uid).then(() => {
			return loadUserSetting(sid, uid)
		}).catch(() => {}).finally(() => {
			uni.hideLoading()
		})
	})

	onShareAppMessage(async (res) => {
		if (res.from === 'button') {
			uni.showLoading({ title: '请稍候……' })
			try {
				const user = res.target.dataset.user || {}
				const obj = buildShareObject(setting.value, user)
				const imageUrl = await shareCanvas.value.buildPreviewImage(obj)
				return {
					title: `${user.nickname}的名片`,
					path: `/pages/share/share?uid=${user.id}&sid=${setting.value.id}`,
					imageUrl,
				}
			} finally {
				uni.hideLoading()
			}
		} else {
			return {
				title: '中路建设（云南）有限公司',
				path: '/pages/index/index',
				imageUrl: '/static/logo.png'
			}
		}
	})
</script>

<style lang="scss" scoped>
	.user-card {
		display: flex;
		flex-direction: column;
		gap: 10rpx;
	}

	.title {
		display: flex;
		align-items: center;
		gap: 40rpx;
		padding: 40rpx;

		&>.logo {
			width: 42.5rpx;
			height: 50rpx;
			margin-top: 10rpx;
		}

		&>.text {
			flex: 1;
			display: flex;
			flex-direction: column;

			&>.sub {
				font-size: 20rpx;
				color: #aaaaaa
			}
		}
	}
</style>